package com.bw.user.controller;

import com.alibaba.fastjson.TypeReference;
import com.bw.user.entity.BargainOrderEntity;
import com.bw.user.entity.from.BargainFrom;
import com.bw.user.entity.to.CutRecordTo;
import com.bw.user.feign.FeignBargainServer;
import com.bw.user.service.BargainOrderService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 
 *
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-03-11 16:07:22
 */
@RestController
@RequestMapping("user/bargain-order")
public class BargainOrderController {
    @Autowired
    private BargainOrderService bargainOrderService;

    @Autowired
    private FeignBargainServer feignBargainServer;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bargainOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{bargainOrderId}")
    public R info(@PathVariable("bargainOrderId") Long bargainOrderId){
		BargainOrderEntity bargainOrder = bargainOrderService.getById(bargainOrderId);

        return R.ok().put("bargainOrder", bargainOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BargainOrderEntity bargainOrder){
        bargainOrder.setCreateTime(new Date());
		bargainOrderService.save(bargainOrder);
		//feign查询砍价商品
        R info = feignBargainServer.info(bargainOrder.getBargainId());
        BargainFrom bargain = info.getData("bargain", new TypeReference<BargainFrom>() {
        });
        // 添加订单 自己直接砍刀
        CutRecordTo cutRecordTo = new CutRecordTo();
        cutRecordTo.setBargainId(bargainOrder.getBargainId());
        cutRecordTo.setCutCount(1);
        cutRecordTo.setCutPrice(bargain.getKnifePrice());
        cutRecordTo.setUserId(bargainOrder.getUserId());
        cutRecordTo.setHelpId(bargainOrder.getUserId());
        feignBargainServer.save (cutRecordTo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BargainOrderEntity bargainOrder){
		bargainOrderService.updateById(bargainOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] bargainOrderIds){
		bargainOrderService.removeByIds(Arrays.asList(bargainOrderIds));

        return R.ok();
    }

}
